//
//  ShellFriendCell.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/26.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellFriendCell.h"
#import "ShellFriendModel.h"


@interface ShellFriendCell()

//头像
@property (nonatomic,strong) UIImageView *headImageView;
//邮箱
@property (nonatomic,strong) UILabel *emailLabel;
//信用
@property (nonatomic,strong) UILabel *creditLabel;
//好友
@property (nonatomic,strong) UILabel *friendLabel;
//算力
@property (nonatomic,strong) UILabel *calculateLabel;

//等级
@property (nonatomic,strong) UILabel *levelBtn;
@property (nonatomic,strong) UILabel *levelLabel;

//质押的数量
@property (nonatomic,strong) UILabel *pledgeNumLab;


@end

@implementation ShellFriendCell



- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = UIColorHex(0x031D32);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType  = UITableViewCellAccessoryNone;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    
    //头像
    [self.contentView addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(25);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    self.headImageView.cornerRadius = 20;
    
    //邮箱
    [self.contentView addSubview:self.emailLabel];
    [self.emailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.left.mas_equalTo(self.headImageView.mas_right).mas_offset(35);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(18);
    }];
    
    
    
    
    //信用
    [self.contentView addSubview:self.creditLabel];
    [self.creditLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.emailLabel.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(self.headImageView.mas_right).mas_offset(35);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(18);
    }];
    
    //质押数量
     [self.contentView addSubview:self.pledgeNumLab];
     [self.pledgeNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.mas_equalTo(self.creditLabel.mas_bottom).mas_offset(9);
         make.left.mas_equalTo(self.headImageView.mas_right).mas_offset(35);
         make.width.mas_equalTo(200);
         make.height.mas_equalTo(18);
     }];
    
    
    //好友
    [self.contentView addSubview:self.friendLabel];
    [self.friendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.creditLabel.mas_centerY);
        make.right.mas_equalTo(-25);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(18);
    }];
    
    
    //算力
    [self.contentView addSubview:self.calculateLabel];
    [self.calculateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pledgeNumLab.mas_centerY);
        make.right.mas_equalTo(-25);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(18);
    }];

    
   
    
    self.bottomLineView.hidden = NO;
    
    
    
    
    ///!!!:等级
    UIButton *levelBtn = [UIButton lz_buttonImageName:@"" backgroundImageName:@""];
    self.levelBtn = levelBtn;
    [levelBtn setGradientBackgroundWithColors:@[UIColorHex(0xFFE482),UIColorHex(0xFF9559)] locations:@[@(0),@(1.0)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [self.contentView addSubview:levelBtn];
    [levelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(self.headImageView.mas_bottom).mas_offset(9);
       make.width.mas_equalTo(70);
       make.height.mas_equalTo(20);
       make.left.mas_equalTo(10);
    }];
    levelBtn.cornerRadius = 10;

   
   UIImageView *levelImgView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_zuanshi")];
   [levelBtn addSubview:levelImgView];
   [levelImgView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerY.mas_equalTo(levelBtn.mas_centerY);
       make.width.mas_equalTo(20);
       make.height.mas_equalTo(15);
       make.left.mas_equalTo(6);
   }];

   [levelBtn addSubview:self.levelLabel];
   [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerY.mas_equalTo(levelBtn.mas_centerY);
       make.width.mas_equalTo(45);
       make.height.mas_equalTo(17);
       make.left.mas_equalTo(levelImgView.mas_right).mas_offset(2);
   }];
    
}


#pragma mark - actions

#pragma mark - custool

#pragma mark - get && set
- (void)setModelObject:(id)modelObject{
    ShellFriendModel *friendModel = modelObject;
    
    self.emailLabel.text = friendModel.email;
    
    
    //信用
    NSString *creditString = [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"信用分"),friendModel.creditScore];
    NSMutableAttributedString *creditAttributed = [NSMutableAttributedString attributedString:creditString
                                                                                     rangeTitle:LZLocalizedString(@"信用分")
                                                                                       leftFont:k_textFont(13)
                                                                                      rightFont:k_textFont(13)
                                                                                      leftColor:RGBA(255, 255, 255, 1)
                                                                                     rightColor:UIColorHex(0x8D99B2)];
    
    self.creditLabel.attributedText = creditAttributed;
    
    
    
    //好友
    NSString *friendString = [NSString stringWithFormat:@"%@ %ld",LZLocalizedString(@"好友"),(long)friendModel.invitedCount];
    NSMutableAttributedString *friendAttributed = [NSMutableAttributedString attributedString:friendString
                                                                                   rangeTitle:LZLocalizedString(@"好友")
                                                                                     leftFont:k_textFont(13)
                                                                                    rightFont:k_textFont(13)
                                                                                    leftColor:UIColorHex(0xFFFFFF)
                                                                                   rightColor:UIColorHex(0x8D99B2)];
    
    self.friendLabel.attributedText = friendAttributed;
    
    //算力
    NSString *calculateString = [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"算力(UG)"),friendModel.hashRate];
    NSMutableAttributedString *calculateAttributed = [NSMutableAttributedString attributedString:calculateString
                                                                                   rangeTitle:LZLocalizedString(@"算力(UG)")
                                                                                     leftFont:k_textFont(13)
                                                                                    rightFont:k_textFont(13)
                                                                                    leftColor:UIColorHex(0xFFFFFF)
                                                                                   rightColor:UIColorHex(0x8D99B2)];
    
    self.calculateLabel.attributedText = calculateAttributed;
    
    
    //质押金额
    NSString *pledgeAmount = [NSString stringWithFormat:@"%f",friendModel.pledgeAmount].saveDecimal;
    NSString *pledgeNumString = [NSString stringWithFormat:@"%@ %@",LZLocalizedString(@"质押数量"),pledgeAmount];
    NSMutableAttributedString *pledgeNumAttributed = [NSMutableAttributedString attributedString:pledgeNumString
                                                                                     rangeTitle:LZLocalizedString(@"质押数量")
                                                                                       leftFont:k_textFont(13)
                                                                                      rightFont:k_textFont(13)
                                                                                      leftColor:UIColorHex(0xFFFFFF)
                                                                                     rightColor:UIColorHex(0x8D99B2)];
      
    self.pledgeNumLab.attributedText = pledgeNumAttributed;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:friendModel.portrait] placeholderImage:k_imageName(@"default_headportrait")];
    
    if ([friendModel.showLevel isEqualToString:@"Y"]) {
        self.levelBtn.hidden = NO;
        NSInteger level = friendModel.level;
        NSString *levelString = @"";
        if (level < 5) {
            levelString = [NSString stringWithFormat:@"B lv%ld",level];
        } else {
            levelString = [NSString stringWithFormat:@"S lv%ld",level-10];
        }
        self.levelLabel.text = levelString;
        
        [self.headImageView mas_updateConstraints:^(MASConstraintMaker *make) {
              make.top.mas_equalTo(15);
        }];
        
        
    } else {
        self.levelBtn.hidden = YES;
        [self.headImageView mas_updateConstraints:^(MASConstraintMaker *make) {
                 make.top.mas_equalTo(30);
        }];
    }

    
    
    
    
}
- (UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
    }
    return _headImageView;
}

//邮箱
- (UILabel *)emailLabel{
    if(!_emailLabel){
        _emailLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(13)];
    }
    return _emailLabel;
}


//信用
- (UILabel *)creditLabel{
    if(!_creditLabel){
        _creditLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(13)];
    }
    return _creditLabel;
}

//好友
- (UILabel *)friendLabel{
    if(!_friendLabel){
        _friendLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(13)];
        [_friendLabel lz_rightAlignment];
    }
    return _friendLabel;
}
//算力
- (UILabel *)calculateLabel{
    if(!_calculateLabel){
        _calculateLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(13)];
        [_calculateLabel lz_rightAlignment];
        _calculateLabel.numberOfLines = 0;
        _calculateLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _calculateLabel;
}

//等级
- (UILabel *)levelLabel{
    if (!_levelLabel) {
        _levelLabel = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(12)];
    }
    return _levelLabel;
}


//质押的数量
- (UILabel *)pledgeNumLab{
    if (!_pledgeNumLab) {
      _pledgeNumLab = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(13)];
    }
    return _pledgeNumLab;
}

@end
